package com.toommi.refuse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.entity.Notice;
import com.toommi.refuse.mapper.NoticeMapper;
import com.toommi.refuse.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: NoticeServiceImpl
 * @Description: TODO
 * @Auther: HuangBo
 * @Date: 2019/9/20 0020 18:10
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 功能描述: 根据id更新文案
     *
     * @param: [notice]
     * @return: void
     * @auther: HuangBo
     * @date: 2019/9/27 0027 下午 13:13
     */
    @Override
    public void updateNoticeById(Notice notice) {
        //判断状态是否为启用
        if (notice.getStatus() == 1) {
            //共同一个页面，只能启用一个文案
            noticeMapper.updatestatusByPageAndId(notice.getId(),notice.getPage());
        }
        notice.setUpdateTime(new Date());
        noticeMapper.updateNoticeById(notice);
    }

    /**
     * 功能描述: 根据id查询文案
     *
     * @param: [id]
     * @return: com.toommi.refuse.entity.Notice
     * @auther: HuangBo
     * @date: 2019/9/27 0027 下午 13:13
     */
    @Override
    public Notice findOneById(String id) {
        return noticeMapper.findOneById(id);
    }

    /**
     * 功能描述: 新增文案
     *
     * @param: [notice]
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @auther: HuangBo
     * @date: 2019/9/27 0027 上午 10:24
     */
    @Override
    public Map<String, Object> addNotice(Notice notice) {
        Map<String, Object> res = new HashMap<>();
        if (notice == null) {
            res.put("code", 204);
            res.put("msg", "新增失败：无效数据.");
            return res;
        } else {
            notice.setCreateTime(new Date());
            notice.setStatus(0);
            noticeMapper.addNotice(notice);
            res.put("code", 0);
            res.put("msg", "添加成功.");
            return res;
        }
    }

    /**
     * 功能描述: 根据id删除文案
     *
     * @param: [id]
     * @return: void
     * @auther: HuangBo
     * @date: 2019/9/27 0027 下午 12:37
     */
    @Override
    public void deleteNoticeById(String id) {
        noticeMapper.deleteNoticeById(id);
    }

    /**
     * 功能描述: 根据条件分页获取文案
     *
     * @param: [page, limit, usepage]
     * @return: java.lang.Object
     * @auther: HuangBo
     * @date: 2019/9/27 0027 上午 10:24
     */
    @Override
    public Object findAll(Integer page, Integer limit, Integer usepage) {
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> all = noticeMapper.findAll(usepage);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(all);
        Map<String, Object> res = new HashMap<>();
        res.put("count", pageInfo.getTotal());
        res.put("code", 0);
        res.put("data", all);
        res.put("msg", "");
        return res;
    }

    @Override
    public String getContentByPage(Integer page) {
        return noticeMapper.getContentByPage(page);
    }

}
